const db=require('./database')
const constant=require('../constant')
const moment=require('moment')
const config=require('../../config')

module.exports={
    CreateNote:async function(ownerid,title){
        await db.InsertAInT('notes',{'ownerid':ownerid,'updatetime':moment(Date.now()).format(config.site.time_format),'title':title})
        let ss=await db.GetAByBInT('notes',['id'],{});
        return ss[ss.length-1].id;
    },
    UpdateNote:async function(id,title,content){
        await db.UpdateAToBInT('notes',{'id':id},{'content':content,'updatetime':moment(Date.now()).format(config.site.time_format),'title':title})
    },
    DeleteNote:async function(id){
        await db.DeleteAInT('notes',{'id':id})
    },
    GetNoteList:async function(ownerid){
        return await db.GetAByBInT('notes',['id','ownerid','title','updatetime'],{'ownerid':ownerid});
    },
    GetContent:async function(id){
        let res=await db.GetAByBInT('notes',['id','content','title','updatetime','ownerid'],{'id':id})
        if(res.length==0){
            throw constant.ERR_NONEXISTENT_NOTE
        }else if(res.length>1){
            throw constant.ERR_REPEATED_NOTES
        }else{
            return res[0]
        }
    }
}